package view.audit
{
	import business.UserDelegate;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import model.AppCons;
	import model.ModelLocator;
	
	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.DataGrid;
	import mx.controls.LinkButton;
	import mx.controls.RadioButton;
	import mx.controls.TextArea;
	import mx.rpc.events.FaultEvent;
	
	import util.LogUtil;
	
	[Bindable]
	public class ApplySuperUserAuditClass extends VBox
	{
		public var m_items:ArrayCollection;
		
		//page 
		public var pageIndices:Array;
		public var page:int;
		public var pagecnt:int;
	    public var pageVolume:int = 10;	
	    
	    public var currentStatus:String;
	    public static const STATUS_WAIT:String = '0';
	    public static const STATUS_PASSED:String = '1';
	    public static const STATUS_REJECT:String = '2';
	    
	    //view
	    public var myGrid:DataGrid;
		public var item_audit:RadioButton;
		public var item_delete:RadioButton;
		public var item_mgs:TextArea;
		
		private var delegate:UserDelegate;
		
		public function ApplySuperUserAuditClass()
		{
			super();
			
			delegate = new UserDelegate( this );
			this.page = 1;
			currentStatus = STATUS_WAIT;
		}
		
		public function onShowChange( event:Event, status:String ):void
		{
			if( event.target.selected )
			{
				currentStatus = status;
				getPage( 1, status );
			}
		}
		
		public function onRefreshClick():void
		{
			getPage( this.page, currentStatus );
		}
		
		public function goto( evt:MouseEvent ):void
		{
			var page:int = LinkButton(evt.currentTarget).data.page;
			
			getPage(page, currentStatus);
		}
		
		public function getPage( page:int, status:String ):void
		{
			delegate.getApplySuperList( page, status, this.pageVolume );
		}
		
		public function getApplySuperListResult(result:Object, token:Object = null):void
		{
			if(result.result.code == '0')
			{
				prepareData( result.result );
			}
		}
		
		public function pastApplyResult( result:Object, token:Object = null ):void
		{
			if( result.result.code == '0' )
			{
				for each( var item:Object in m_items )
				{
					if( item.bid == result.result.bid )
					{
						item.status = result.result.status;
						return ;
					}					
				}
			}
		}
		
		public function fault(error:FaultEvent,token:Object = null):void
		{
			Alert.show(error.fault.faultString,'error');
		} 
		
	 	public function onLabelSpaceClick():void
	 	{
	 		var u:URLRequest = new URLRequest(myGrid.selectedItem.url); 
	 		navigateToURL(u,"_blank"); 
	 	}		
		
		private function prepareData( result:Object ):void
		{
			this.m_items = new ArrayCollection(result.data);
			
			for each( var item:Object in m_items )
			{
				if( item.gender == '1' )
				{
					item.genderName = '男';
				}
				else if( item.gender == '2' )
				{
					item.genderName == '女';
				}
				else
				{
					item.genderName == '不详';
				}
				
				item.assetCount = int(item.bookCount) + int(item.cardCount);
				item.allScore = int(item.createScore) + int(item.socialScore); 
			}
			
			//显示分页
			this.page = result.page>0?result.page:0;
			this.pagecnt = result.pagecnt>1?result.pagecnt:1;
			this.makePageIndices();				
		}
		
		public function gotoPage(page:String):void
		{
			if (page.length>0)
			{
				getPage(int(page), currentStatus);
			}
		}	
		
		public function onDataGridItemClick():void
		{
			if( this.myGrid.selectedItem.status == STATUS_REJECT )
			{
				item_delete.selected = true; 
			}
			else
			{
				item_audit.selected = true;
			}
		}
		
		public function getStatusTip( st:String ):String
		{
			if( st == STATUS_WAIT )
			{
				return '未审核';
			}	
			else if( st == STATUS_PASSED )
			{
				return '通过';
			}
			else
			{		
				return '拒绝';
			}
		}
		
		public function onSubmit( event:Event ):void
		{
			var stat:String = item_audit.selected?STATUS_PASSED:STATUS_REJECT;
			setSuperUser( this.myGrid.selectedItem.bid, stat, item_mgs.text );
		}
		
		public function setSuperUser( bid:String, stat:String, msg:String ):void
		{
			delegate.pastApply( bid, stat, msg );
			
			var obj:Object = new Object();
			obj.bid = bid;
			obj.editStatus = stat;
			obj.msg = msg; 
			LogUtil.getInstance().log(ModelLocator.getInstance().adminUserVO.id, ModelLocator.getInstance().adminUserVO.name, AppCons.USER, LogUtil.UPDATE, obj, '', '', true);			
		}
		
		private function makePageIndices():void
		{
			var a:Array = new Array();
			a.push({page:1,desc:'首页'});			
			a.push({page:page-1,desc:'前一页'});			
			a.push({page:page+1,desc:'后一页'});
			a.push({page:pagecnt,desc:'尾页'});
			this.pageIndices = a;
		} 		
		
	}
}